home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Programmer Disk
/
The Programmer Disk (Microforum).iso
/
xpro
/
c
/
pro19
/
fgets.c
< prev
next >
Wrap
Text File
|
1987-10-04
|
2KB
|
61 lines
#define NOCCARGC /* no arg count passing */
#include stdio.h
#include clib.def
/*
** Gets an entire string (including its newline
** terminator) or size-1 characters, whichever comes
** first. The input is terminated by a null character.
** Entry: str = Pointer to destination buffer.
** size = Size of the destination buffer.
** fd = File descriptor of pertinent file.
** Returns str on success, else NULL.
*/
fgets(str, size, fd) char *str; int size, fd; {
return (Ugets(str, size, fd, 1));
}
/*
** Gets an entire string from stdin (excluding its newline
** terminator) or size-1 characters, whichever comes
** first. The input is terminated by a null character.
** The user buffer must be large enough to hold the data.
** Entry: str = Pointer to destination buffer.
** Returns str on success, else NULL.
*/
gets(str) char *str; {
return (Ugets(str, 32767, stdin, 0));
}
Ugets(str, size, fd, nl) char *str; int size, fd, nl; {
int backup;
char *next;
next = str;
while(--size > 0) {
switch (*next = fgetc(fd)) {
case EOF: *next = NULL;
if(next == str) return (NULL);
return (str);
case '\n': *(next + nl) = NULL;
return (str);
case RUB: if(next > str) backup = 1; else backup = 0;
goto backout;
case WIPE: backup = next - str;
backout:
if(iscons(fd)) {
fputs("\b \b\b \b", stderr);
++size;
while(backup--) {
fputs("\b \b", stderr);
if(*--next < 32) fputs("\b \b", stderr);
++size;
}
continue;
}
default: ++next;
}
}
*next = NULL;
return (str);
}